Completed
Push — master ( 430c18...52d1c6 )
by
unknown
54s
created

save.js ➔ ... ➔ ???   C

Complexity

Conditions 10
Paths 10

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 10
dl 0
loc 23
rs 5.6534
c 0
b 0
f 0
cc 10
nop 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A save.js ➔ saveHtml 0 6 1

How to fix   Complexity   

Complexity

Complex classes like save.js ➔ ... ➔ ??? often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import fse from 'fs-extra'
2
import mkdirp from 'mkdirp'
3
import xss from 'xss'
4
import path from 'path'
5
6
import {
7
  config
8
} from '../../'
9
10
export function saveJson(url, json) {
11
  mkdirp.sync(path.dirname(url))
12
13
  if(typeof json.abe_source !== 'undefined' && json.abe_source !== null) {
14
    delete json.abe_source
15
  }
16
17
  var eachRecursive = function (obj) {
18
    for (var k in obj) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
19
      if (typeof obj[k] === 'object' && obj[k] !== null){
20
        eachRecursive(obj[k])
21
      } else if (typeof obj[k] !== 'undefined' && obj[k] !== null){
22
        obj[k] = xss(obj[k].toString().replace(/"/g, '"'), { 'whiteList': config.htmlWhiteList })
23
      }
24
    }
25
  }
26
27
  eachRecursive(json)
28
29
  fse.writeJsonSync(url, json, {
30
    space: 2,
31
    encoding: 'utf-8'
32
  })
33
  return true
34
}
35
36
export function saveHtml(url, html) {
37
  mkdirp.sync(path.dirname(url))
38
  fse.writeFileSync(url, html)
39
40
  return true
41
}